package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IMrWardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MrDepts;
import com.ruoyi.system.service.IMrDeptsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域Controller
 *
 * @author lsw
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/system/depts")
public class MrDeptsController extends BaseController
{
    @Autowired
    private IMrDeptsService mrDeptsService;

    @Autowired
    private IMrWardService mrWardService;

    /**
     * 查询区域列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MrDepts mrDepts)
    {
        startPage();
        List<MrDepts> list = mrDeptsService.selectMrDeptsList(mrDepts);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:depts:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MrDepts mrDepts)
    {
        List<MrDepts> list = mrDeptsService.selectMrDeptsList(mrDepts);
        ExcelUtil<MrDepts> util = new ExcelUtil<MrDepts>(MrDepts.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:depts:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        return success(mrDeptsService.selectMrDeptsByDeptId(deptId));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:depts:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MrDepts mrDepts)
    {
        String deptCode = mrDepts.getDeptCode();
        if(StringUtils.isEmpty(deptCode)){
            return error("区域编码不能为空");
        }
        int count = mrDeptsService.checkDeptCodes(mrDepts);
        if(count > 0){
            return error("区域编码重复");
        }
        return toAjax(mrDeptsService.insertMrDepts(mrDepts));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:depts:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MrDepts mrDepts)
    {
        String deptCode = mrDepts.getDeptCode();
        if(StringUtils.isEmpty(deptCode)){
            return error("区域编码不能为空");
        }
        int count = mrDeptsService.checkDeptCodes(mrDepts);
        if(count > 0){
            return error("区域编码重复");
        }
        return toAjax(mrDeptsService.updateMrDepts(mrDepts));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:depts:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        int bandBed = mrWardService.checkDeptBandWard(deptIds);
        if(bandBed > 0){
            return error("区域使用中，请先删除网关！");
        } else {
            return toAjax(mrDeptsService.deleteMrDeptsByDeptIds(deptIds));
        }
    }
}
